package com.zn.opit.quartzdemo.domain.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Slf4j
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class BaseJob implements Job, Serializable {

    @Override
    public void execute(JobExecutionContext context){
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();
        // 获取任务中保存的方法名字，动态调用方法
        String methodName = dataMap.getString("methodName");
        try {
            Method method = this.getClass().getMethod(methodName);
            method.invoke(this);
        } catch (NoSuchMethodException e) {
            log.error("{} not exists method {}", this.getClass().getName(), methodName, e);
        } catch (InvocationTargetException e) {
            log.error("{} invoke method {} failed", this.getClass().getName(), methodName, e);
        } catch (IllegalAccessException e) {
            log.error("Illegal Access", e);
        }
    }
}
